package nl.boelen.game.net.packet;

public class Packet20Chunk extends Packet {

    private static final int packetNumber = 20;
    private int xChunk;
    private int yChunk;
    private int[] chunkData;

    public Packet20Chunk(byte[] data) {
        super(packetNumber);
        String[] datas = readData(data).split(":");
        this.xChunk = Integer.parseInt(datas[0]);
        this.yChunk = Integer.parseInt(datas[1]);
        this.chunkData = chunkDataToArray(datas[2]);
    }

    public Packet20Chunk(int x, int y, int[] chunkData) {
        super(packetNumber);
        this.xChunk = x;
        this.yChunk = y;
        this.chunkData = chunkData;
    }

    @Override
    public byte[] getData() {
        return (packetNumber + this.xChunk + ":" + this.yChunk + ":" + getChunkDataAsString()).getBytes();
    }

    public String getChunkDataAsString() {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < chunkData.length; i++) {
            sb.append(chunkData[i]);
            if (i < chunkData.length) {
                sb.append(",");
            }
        }
        System.out.println(sb.toString());
        return sb.toString();
    }

    private int[] chunkDataToArray(String string) {
        String[] d = string.split(",");
        int[] data = new int[d.length];
        for (int i = 0; i < d.length; i++) {
            data[i] = Integer.parseInt(d[i]);
        }
        return data;
    }

    public int getXChunk() {
        return xChunk;
    }

    public int getYChunk() {
        return yChunk;
    }

    public int[] getChunkData() {
        return chunkData;
    }
}